﻿using UnityEngine;
using System.Collections;
using System.Reflection;

namespace System.Extension
{
    public static class TypeExtension
    {
        #region Reflection
        static BindingFlags preferedFlag = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Static;

        public static void ForEachFieldInfo(this Type type, Action<FieldInfo> callBack)
        {
            FieldInfo[] fieldInfos = type.GetFields(preferedFlag);
            foreach (FieldInfo fieldInfo in fieldInfos)
                callBack(fieldInfo);
        }

        public static void ForEachPropertyInfo(this Type type, Action<PropertyInfo> callBack)
        {
            PropertyInfo[] propertyInfos = type.GetProperties(preferedFlag);
            foreach (PropertyInfo propertyInfo in propertyInfos)
                callBack(propertyInfo);
        }

        public static void ForEachMemberInfo(this Type type, Action<MemberInfo> callBack)
        {
            type.ForEachFieldInfo(f => callBack(f));
            type.ForEachPropertyInfo(p => callBack(p));
        }

        public static void ForEachMethodInfo(this Type type, Action<MethodInfo> callBack)
        {
            MethodInfo[] methodInfos = type.GetMethods(preferedFlag);
            foreach (MethodInfo methodInfo in methodInfos)
                callBack(methodInfo);
        }

        public static MethodInfo GetMethodInfo(this Type type, string methodName)
        {
            return type.GetMethod(methodName, preferedFlag);
        }

        #endregion
    }

}